package cn.ncist.health_backend.controller;

import cn.ncist.common.RespResult;
import cn.ncist.constant.MessageConstant;
import cn.ncist.entity.QueryPageBean;
import cn.ncist.health_interface.service.CheckItemService;
import cn.ncist.pojo.CheckItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查项管理
 */
@Slf4j
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @DubboReference   // 此注解是dubbo提供的，含义是在zookeeper注册中心寻找服务接口
    private CheckItemService checkItemService;

    /**
     * 添加检查项
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @PostMapping("/add")
    public RespResult add(@RequestBody CheckItem checkItem) {
//        log.info("接收到请求了...");
        try {
            checkItemService.add(checkItem);
            return new RespResult(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            log.info("addCheckItem失败了");
            e.printStackTrace();
            return new RespResult(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @PostMapping("/page")
    public RespResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Page<CheckItem> pageInfo = checkItemService.findPage(queryPageBean);
            return new RespResult(true,"查询检查项成功",pageInfo);
        } catch (Exception e) {
            log.info("checkItem分页失败");
            e.printStackTrace();
            return new RespResult(false,"查询检查项失败");
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @GetMapping("/delete")
    public RespResult delete(Integer id){
        try {
            checkItemService.delete(id);
            return new RespResult(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            log.info("删除检查项失败");
            e.printStackTrace();
            return new RespResult(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @PostMapping("/update")
    public RespResult update(@RequestBody CheckItem checkItem){
        log.info(checkItem.toString());
        try {
            checkItemService.updateItem(checkItem);
            return new RespResult(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("/getAll")
    public RespResult findALl(){
        List<CheckItem> checkItemList = checkItemService.findAll();
        if (checkItemList.size() > 0 && checkItemList != null){
            return new RespResult(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        }else {
            return new RespResult(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
